package app

import (
	"fmt"
	"net/http"
	"strconv"
)

// URLLogin 登录
func URLLogin(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := LoginBody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	name, err := Service.User.LoginUser(body.ID, body.Password)
	fmt.Println(name)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send(name, true)
}

// URLGetUserInfo 根据ID获取用户信息
func URLGetUserInfo(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	id := handler.DecodePath(2)
	UserInfo, err := Service.User.GetUserByID(id)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send(UserInfo, true)
}

// URLSignup 根据上传的内容进行注册
func URLSignup(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := RegistBody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	id, err := Service.User.CreateUser(body.ID, body.Name, body.Password)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	fmt.Printf("Signup Successfully %s", id)
	handler.Send("success", true)
}

// URLAddOnetime 根据请求往数据库中添加一个一次性事件
func URLAddOnetime(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := OnetimeBody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	newOnetime := Service.Onetime.CreateOnetime(body)
	err = Service.Onetime.PostOnetime(newOnetime)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send("success", true)
}

// URLDeleteOnetime 根据URL的请求从数据库中删除一个一次性事件
func URLDeleteOnetime(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	id := handler.DecodePath(2)
	err := Service.Onetime.DeleteOnetime(id)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send("success", true)
}

// URLGetOnetimeByID 根据用户的ID获取所有的一次性事件
func URLGetOnetimeByID(w http.ResponseWriter, r *http.Request) {
	// handler := CreateHandler(w, r)
	handler := CreateHandler(w, r)
	id := handler.DecodePath(2)
	onetimes, err := Service.Onetime.GetOnetimeByUserID(id)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send(onetimes, true)
}

// URLGetSpecialOnetime 获取特定的一次性事件
func URLGetSpecialOnetime(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	id := handler.DecodePath(2)
	year, err := strconv.Atoi((handler.DecodePath(3)))
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	month, err := strconv.Atoi(handler.DecodePath(4))
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	day, err := strconv.Atoi(handler.DecodePath(5))
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	onetimes, err := Service.Onetime.GetOnetimeBytimestamp(id, year, month, day)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send(onetimes, true)
}

// URLAddDaily 根据URL的请求往数据库中增加一个日常事件
func URLAddDaily(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := DailyBody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	newDaily := Service.Daily.CreateDaily(body.OwnID, body.Hour, body.Min, body.Content)
	Service.Daily.PostDaily(newDaily)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send("success", true)
}

// URLDeleteDaily 根据URL的请求从数据库中删除一个一次性事件
func URLDeleteDaily(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	id := handler.DecodePath(2)
	err := Service.Daily.DeleteDaily(id)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send("success", true)
}

// URLGetDailyByID 根据用户的ID获取所有的日常事件
func URLGetDailyByID(w http.ResponseWriter, r *http.Request) {
	// handler := CreateHandler(w, r)
	handler := CreateHandler(w, r)
	id := handler.DecodePath(2)
	dailys, err := Service.Daily.GetDailyByUserID(id)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send(dailys, true)
}

// URLAddWeekly 根据请求增加一个周常事件
func URLAddWeekly(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	body := WeeklyBody{}
	err := handler.DecodePost(&body)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	newWeekly, err := Service.Weekly.CreateWeekly(body.OwnID, body.Week, body.Hour, body.Min, body.Content)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	Service.Weekly.PostWeekly(newWeekly)
	handler.Send("success", true)
}

// URLDeleteWeekly 根据URL请求删除一个周常事件
func URLDeleteWeekly(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	id := handler.DecodePath(2)
	err := Service.Weekly.DeleteWeekly(id)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send("success", true)
}

// URLGetWeeklyByID 根据用户的ID获取所有的周常事件
func URLGetWeeklyByID(w http.ResponseWriter, r *http.Request) {
	handler := CreateHandler(w, r)
	id := handler.DecodePath(2)
	weeklys, err := Service.Weekly.GetWeeklyByUserID(id)
	if err != nil {
		handler.Send(err.Error(), false)
		return
	}
	handler.Send(weeklys, true)
}
